// 组件开发
// http://doc.go-admin.cn/zh/development/template/components/

package wangeditor

import (
	_ "embed"
	"html/template"
	"path/filepath"

	adminTemplate "github.com/GoAdminGroup/go-admin/template"
)

//go:embed wang_editor.tmpl
var htmlData string

//go:embed assets/wangeditor5.min.css
var css []byte

//go:embed assets/wangeditor5.min.js
var js []byte

type WangEditor struct {
	*adminTemplate.BaseComponent
}

func New() *WangEditor {
	return &WangEditor{
		BaseComponent: &adminTemplate.BaseComponent{
			Name:     "wang_editor",
			HTMLData: htmlData,
		},
	}
}

func (l *WangEditor) GetAssetList() []string {
	return []string{
		"/wangeditor5.min.css",
		"/wangeditor5.min.js",
	}
}

func (l *WangEditor) GetAsset(name string) ([]byte, error) {
	// return os.ReadFile("."+name)
	// println("ext:", filepath.Ext(name))
	switch filepath.Ext(name) {
	case ".css":
		return css, nil
	case ".js":
		return js, nil
	default:
		return []byte{}, nil
	}
}

func (l *WangEditor) GetContent() template.HTML {
	// buffer := new(bytes.Buffer)
	// tmpl, defineName := l.GetTemplate()
	// err := tmpl.ExecuteTemplate(buffer, defineName, l)
	// if err != nil {
	// 	println("cherry editor, compose html error:", err)
	// }
	// return template.HTML(buffer.String())

	// 不能编译模版文件，否则 FormField 无法传递到模版文件
	// http://doc.go-admin.cn/zh/admin/form/components/
	// FieldCustomContent 会将 FormField 传递到模版文件并编译
	return template.HTML(l.HTMLData)
}
